<template>
  <headPage></headPage>
  <div class="main_box information">
    <div class="title_info">
      <img
        @click="navigateBack"
        class="info_1"
        src="../../../assets/my/14.png"
        alt=""
      />
      <div class="info_2">提现申请</div>
      <img
        @click="showAgreement"
        class="info_3"
        src="../../../assets/my/17.png"
        alt=""
      />
    </div>
    <div class="tips" v-html="agreementContent"></div>
    <div class="tixian_title">提现金额</div>
    <div class="tixian_input">
      <div class="tixian_input_1">￥</div>
      <div class="tixian_input_2">
        <el-input
          type="number"
          v-model="money"
          :placeholder="`可提现金额￥${totalMoney}`"
          placeholder-style="color:#bfbfbf;font-size:7px;"
          @input="inputChange"
          onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)));"
        />
      </div>
      <div class="tixian_input_3" @click="setMoney">全部</div>
    </div>
    <div class="tixian_fees">
      提现需收取税金{{
        Number(methodsInformation.method_fee)
      }}%，平台5个工作日内处理提现申请
    </div>
    <div class="tixian_title">提现方式</div>
    <div class="types">
      <div
        class="types_item"
        :class="item.active ? 'active' : ''"
        v-for="(item, index) in types"
        :key="index"
        @click="select(index)"
      >
        <img :src="quireImg(item.icon)" alt="" />
        <span>{{ item.name }}</span>
      </div>
    </div>
    <div class="save_btn" @click="immediateWithdrawal">立即提现</div>
    <div class="line"></div>
    <div class="detail_title">
      <div class="title_left">提现明细</div>
      <div class="title_right">
        <div class="right_date">
          <el-date-picker
            v-model="date"
            :clearable="false"
            type="daterange"
            size="small"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="dateChange"
            range-separator=" - "
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <img src="../../../assets/my/16.png" alt="" />
        </div>
      </div>
    </div>
    <div class="tab_content" v-loading="loading">
      <div class="table_th">
        <div class="th_item th_w_1">时间</div>
        <div class="th_item th_w_2">状态</div>
        <div class="th_item th_w_3">账户余额</div>
        <div class="th_item th_w_4">描述</div>
      </div>
      <div class="table_td" v-for="(item, index) in tableData" :key="index">
        <div class="td_item th_w_1">{{ item.create_time }}</div>
        <div
          class="td_item th_w_2"
          :class="item.check_status == 3 ? 'error' : ''"
        >
          {{ item.check_status_name }}
        </div>
        <div class="td_item th_w_3">-{{ item.cash_fee }}</div>
        <div class="td_item th_w_4">{{ item.check_reason }}</div>
      </div>
    </div>
    <div class="no_data" v-if="tableData.length == 0 && !loading">
      <img class="no_img" src="../../../assets/default/12.png" alt="" />
    </div>
    <paging
      :total="total"
      :pageSize="pageSize"
      @pageChange="pageChange"
    ></paging>
  </div>
  <footerPage></footerPage>

  <maskLayer v-model="visible" center append-to-body>
    <div class="modal_content">
      <div class="tip_title">
        <span>提现密码</span>
        <img @click="closePopup" src="../../../assets/home/14.png" alt="" />
      </div>
      <div class="tip_text">为了您的资金安全，请输入提现密码进行验证</div>
      <div class="psd_value">
        <psdInput @input-complete="psdChange"></psdInput>
      </div>
      <div class="tip_btn">
        <div class="tip_btn_2" @click="confirm">立即提现</div>
      </div>
    </div>
  </maskLayer>

  <maskLayer v-model="visibleSuccess" center append-to-body>
    <div class="modal_content">
      <div class="tip_title">
        <span>提交成功</span>
        <img @click="closeVideo" src="../../../assets/login/9.png" alt="" />
      </div>
      <div class="modal_success">
        <img src="../../../assets/my/23.png" alt="" />
        <div class="success_text_1">申请已提交</div>
        <div class="success_text_2">
          提现申请已提交，平台将在5个工作日内处理提现申请
        </div>
      </div>
      <div class="tip_btn">
        <div class="tip_btn_1" @click="closeVideo">返回</div>
      </div>
    </div>
  </maskLayer>
  <viewAgreement ref="agreement" :score="4"></viewAgreement>
</template>


<script name="withdrawal" setup>
import emitter from "@/utils/mitt.js";
import {
  post641568f1b6f87,
  post65f03ff7def2d,
  post636e4ff0626ef,
  post6620f0214e89f,
} from "@/utils/api.js";
import headPage from "@/components/headPage.vue";
import footerPage from "@/components/footerPage.vue";
import paging from "@/components/paging.vue";
import maskLayer from "@/components/maskLayer.vue";
import psdInput from "@/components/psdInput.vue";
import viewAgreement from "@/components/viewAgreement.vue";

import { ref, reactive, onMounted, inject } from "vue";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
const injectMessage = inject("$message");

// 上一页
function navigateBack() {
  window.history.length > 1 ? router.go(-1) : router.push("/");
}
function quireImg(imgPath) {
  return new URL(`../../../assets/my/${imgPath}`, import.meta.url).href;
}
onMounted(() => {
  getAgreement();
  getTypesInformation();
  getList();
});

let date = ref("");
let money = ref("");
let totalMoney = ref(0);
function setMoney() {
  money.value = totalMoney.value;
}
function inputChange() {
  if (money.value > totalMoney.value) {
    money.value = totalMoney.value;
  }
}

let method_code = ref("wx");
let types = reactive([
  {
    id: "wx",
    name: "微信",
    icon: "18.png",
    active: true,
  },
  {
    id: "alipay",
    name: "支付宝",
    icon: "19.png",
    active: false,
  },
  {
    id: "bank",
    name: "银行卡",
    icon: "20.png",
    active: false,
  },
]);
function select(index) {
  types.forEach((item, i) => {
    if (i == index) {
      item.active = true;
      method_code.value = item.id;
    } else {
      item.active = false;
    }
  });
}
let agreement = ref(null);
function showAgreement() {
  agreement.value.openPopup();
}
let select_time = ref("");
function dateChange(e) {
  select_time.value = e[0] + "/" + e[1];
  loading.value = true;
  getList();
}
// 提现明细
let loading = ref(true);
let pageNum = ref(1);
let pageSize = ref(10);
let total = ref(0);
let tableData = reactive([]);
function getList() {
  post6620f0214e89f({
    create_time: select_time.value,
    page: pageNum.value,
    list_rows: pageSize.value,
  })
    .then((res) => {
      // console.log(res);
      loading.value = false;
      if (res.code == 1) {
        total.value = res.data.total;
        tableData = res.data.data;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
// 分页变化
function pageChange(e) {
  pageNum.value = e;
  loading.value = true;
  getList();
}

// 立即提现
let visibleSuccess = ref(false);
function immediateWithdrawal() {
  if (money.value == 0) {
    injectMessage.error("请输入提现金额");
    return;
  }
  if (money.value < Number(methodsInformation.method_min)) {
    injectMessage.error(
      "提现金额不能小于" + Number(methodsInformation.method_min)
    );
    return;
  }
  post636e4ff0626ef({
    account_type: method_code.value,
    cash_fee: money.value,
  })
    .then((res) => {
      if (res.code == 1) {
        money.value = 0;
        visibleSuccess.value = true;
        emitter.emit("withdrawalSuccess");
      } else if (res.code == -2) {
        router.push({
          path: "/my/account",
          query: {
            account_type: method_code.value,
          },
        });
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
function closeVideo() {
  visibleSuccess.value = false;
}
let visible = ref(false);
function closePopup() {
  visible.value = false;
}
function confirm() {
  visible.value = false;
}

function psdChange(val) {
  console.log(val);
}

// 获取协议内容  post641568f1b6f87
let agreementContent = ref("");
function getAgreement() {
  post641568f1b6f87({
    column_id: 4,
  })
    .then((res) => {
      if (res.code == 1) {
        agreementContent.value = res.data.content;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
// 提现方式信息 post65f03ff7def2d
let methodsInformation = reactive({});
function getTypesInformation() {
  post65f03ff7def2d({
    method_code: method_code.value,
  })
    .then((res) => {
      // console.log(res);
      if (res.code == 1) {
        methodsInformation = res.data;
        totalMoney.value = Number(res.data.user_money);
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
</script>

<style lang="scss" scoped>
.information {
  margin-top: 110px;
  margin-bottom: 24px;
  padding: 18px 16px;
  background: #fff;
  min-height: 358px;

  .title_info {
    display: flex;
    align-items: center;
    .info_1 {
      width: 10px;
      height: 10px;
      cursor: pointer;
    }
    .info_2 {
      font-size: 10px;
      font-weight: 500;
      margin-left: 6px;
    }
    .info_3 {
      width: 8px;
      height: 8px;
      margin-left: 6px;
      cursor: pointer;
    }
  }
  .tips {
    padding: 9px 12px;
    font-size: 7px;
    color: #666666;
    margin-top: 13px;
    background: #f6f7f9;
    border-radius: 3px;
  }
  .tixian_title {
    margin: 18px 0 12px;
    font-size: 9px;
  }
  .tixian_input {
    width: 260px;
    height: 28px;
    background: #f6f7f9;
    border-radius: 3px;
    padding: 0 12px;
    display: flex;
    align-items: center;
    .tixian_input_1 {
      font-size: 10px;
      font-weight: bold;
    }
    .tixian_input_2 {
      font-size: 7px;
      flex: 1;
    }
    .tixian_input_3 {
      color: #1f52ae;
      font-size: 8px;
      cursor: pointer;
    }
  }
  .tixian_fees {
    font-size: 7px;
    color: #999;
    margin-top: 10px;
  }
  .types {
    display: flex;
    align-items: center;
    .types_item {
      display: flex;
      align-items: center;
      font-size: 8px;
      background: url(../../../assets/my/21.png) no-repeat;
      background-size: 100% 100%;
      width: 130px;
      height: 36px;
      margin-right: 8.5px;
      padding-left: 16px;
      border-radius: 3px;
      cursor: pointer;
      &.active {
        border: 1px solid #1f52ae;
      }
      img {
        width: 18px;
        height: 18px;
        margin-right: 6px;
      }
    }
  }
  .save_btn {
    width: 100px;
    height: 24px;
    background: #1f52ae;
    font-size: 8px;
    color: #fff;
    border-radius: 3px;
    margin: 20px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  .line {
    width: 100%;
    height: 3px;
    border-radius: 3px;
    background: #f6f7f9;
  }
  .detail_title {
    margin: 19.5px 0 15.5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .title_left {
      font-size: 9px;
      font-weight: 500;
    }
    .title_right {
      display: flex;
      justify-content: flex-end;
      .right_date {
        font-size: 8px !important;
        border-radius: 2px;
        border: 1px solid #e5e5e5;
        height: 16px;
        width: 150px;
        position: relative;
        img {
          width: 7px;
          height: 7px;
          position: absolute;
          right: 8px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
      .wallet_tab {
        display: flex;
        align-items: center;
        margin-left: 10px;
        .tab_item {
          width: 36px;
          height: 16px;
          background: #f6f7f9;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 8px;
          color: #666666;
          margin-left: 10px;
          cursor: pointer;
          &.active {
            background: #e8edf7;
            color: #1f52ae;
            font-weight: 500;
          }
        }
      }
    }
  }
  .tab_content {
    width: 688px;
    margin: 15.5px 0 8px 0;
    .table_th {
      background: #f6f7f9;
      border-top: 1px solid #eeeeee;
      border-left: 1px solid #eeeeee;
      border-bottom: 1px solid #eeeeee;
      display: flex;
      .th_item {
        padding: 8.5px 12px;
        // padding: 0 12px;
        border-right: 1px solid #eeeeee;
        font-size: 7px;
        color: #666;
      }
    }
    .table_td {
      border-left: 1px solid #eeeeee;
      border-bottom: 1px solid #eeeeee;
      display: flex;
      .td_item {
        padding: 8.5px 12px;
        border-right: 1px solid #eeeeee;
        font-size: 7px;
        display: flex;
        align-items: center;
      }
      .error {
        color: #f43024;
      }
    }
    .th_w_1 {
      width: 131px;
    }
    .th_w_2 {
      width: 172px;
    }
    .th_w_3 {
      width: 194px;
    }
    .th_w_4 {
      width: 190px;
    }
    .table_td:hover {
      background: #f6f7f9;
    }
  }
}
.modal_content {
  background: #fff;
  width: 268px;
  padding-bottom: 16px;
  .tip_title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 9px;
    font-weight: bold;
    height: 28px;
    background: #1f52ae;
    padding: 0 12px 0 16px;
    color: #fff;
    img {
      width: 8px;
      height: 8px;
      cursor: pointer;
    }
  }
  .tip_text {
    font-size: 8px;
    margin: 42px 0 0px;
    color: #666666;
    text-align: center;
  }
  .psd_value {
    display: flex;
    justify-content: center;
    margin: 17.5px 0 36px;
  }
  .modal_success {
    text-align: center;
    padding-top: 35px;
    img {
      width: 40px;
      height: 40px;
    }
    .success_text_1 {
      text-align: center;
      margin: 13px 0 12px;
      font-size: 10px;
      font-weight: 500;
    }
    .success_text_2 {
      margin: 0 64px 28px;
      font-size: 8px;
      color: #666;
      text-align: center;
    }
  }
  .tip_btn {
    display: flex;
    align-items: center;
    justify-content: center;

    .tip_btn_1 {
      width: 100px;
      height: 24px;
      background: #f6f7f9;
      border-radius: 3px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 8px;
      cursor: pointer;
    }
    .tip_btn_2 {
      width: 100px;
      height: 24px;
      background: #1f52ae;
      color: #fff;
      border-radius: 3px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 8px;
      cursor: pointer;
    }
  }
}
</style>